import * as util from './utils.js';
/* 
const foot = {
	name:"红烧狮子头",
	logo:"2020-2-2-0",
	week:0,
	mid:[],
	lat:[],
}

const currentRecord = {
	logo:"2020-2-2-0",
	week:0,
	mid:[],
	lat:[],
}

const footlist = [{name,week,type}] */

/* 获取logo */
function getCurrentlogo(paramDate,paramPlaceHolder){
	console.log("[foot.js]  getCurrentlogo()---- paramDate:",paramDate,"paramPlaceHolder",paramPlaceHolder);
	return util.dateAttributeMergeToString(paramDate,paramPlaceHolder);
}

/* 添加食物 */
function uploadFoot(paramFoots)
{
	let footlist = getFoot();
	if(!paramFoots)
	{
		return;
	}
	
	if(paramFoots.name)
	{
		let names = paramFoots.name.split("\n");
		let spliceIndex = names.findIndex(e=> e =="");
		if(spliceIndex!=-1)
		{
			names.splice(spliceIndex);
		}
		for(let [key, value] of names.entries()){
		    footlist.push({
				name:value,
				type:paramFoots.type,
				week:paramFoots.week,
				nutritionType:paramFoots.nutritionType,
			});
		}
		util.uploadLocalStorage("footlist",footlist);
	}
}

//删除食物
function removeFoot(paramFoot)
{
	console.log("==================================")
	console.log(paramFoot);
	let footlist = getFoot();
	if(footlist.length == 0)
	{
		return;
	}
	
	let spliceIndex = footlist.findIndex(e => (e.name == paramFoot.name && e.type == paramFoot.type && e.week.i == paramFoot.week.i));
	console.log("------------------------------------------------",spliceIndex);
	if(spliceIndex != -1)
	{
		footlist.splice(spliceIndex,1);
	}
	util.uploadLocalStorage("footlist",footlist);
}


//获取所有食物
function getFoot()
{
	let foot = util.getLocalStorage("footlist");
	if(foot)
	{
		console.log("footlist ----------------begin-------------")
		for(let [k,v] of foot.entries() )
		{
			console.log(v);
		}
		console.log("footlist ----------------ending-------------")
		return foot;
	}
	/* let weekArray = new Map();
	for (var i = 0; i < 7; i++) {
		weekArray.set(i,[]);
	}
	foot = new Map();
	foot.set("mid",weekArray);
	foot.set("lat",weekArray);
	util.uploadLocalStorage("foot",foot,"map"); */
	console.log("footlist:",[]);
	return [];
	
}

//展示食物
function showFoot(paramSearch)
{
	
	let footlist = getFoot();
	let restlist = footlist;
	
	if( paramSearch.type && paramSearch.type != "all" )
	{
		restlist = footlist.filter(e => e.type == paramSearch.type);
	}
	if( paramSearch.week!=undefined && paramSearch.week!=null && paramSearch.week!=-1)
	{
		restlist = restlist.filter(e => e.week.i == paramSearch.week);
	}
	if( paramSearch.name && paramSearch.name != "")
	{
		restlist = restlist.filter(e => e.name.indexOf(paramSearch.name) != -1);
	}
	
	
	return restlist;
}

//读取历史记录
function getHistory()
{
	let history = util.getLocalStorage("history");
	if(history)
	{
		return history;
	}
	return [];
}

function showHistory()
{
	let history = util.getLocalStorage("history");
	if(history)
	{
		history = history.filter(e=> e.logo != getCurrentlogo(undefined,"-"));
		return history;
	}
	return [];
}

//上传记录
function uploadHistory(paramHistory)
{
	let history = getHistory();
	history.push(paramHistory);
	util.uploadLocalStorage("history",history);
}

//删除记录
function removeHistory()
{
	let history = getHistory();
	let historyIndex = history.findIndex(e=> e.logo == getCurrentlogo(undefined,"-"));
	if(historyIndex !=-1)
	{
		history.splice(historyIndex,1);
		util.uploadLocalStorage("history",history);
	}
	
}
//得到当天记录
function currentRecord()
{
	removeHistory();
	let history = getHistory();
	
	console.log("=-=-=--=-----------------------------history")
	for( let [k,v] of history.entries())
	{
		console.log(JSON.stringify(v));
	}
	let historyIndex = history.findIndex(e=> e.logo == getCurrentlogo(undefined,"-"));
	let restData;
	if(history.length==0 || historyIndex == -1)
	{
		console.log("-------------------------------------非缓存")
		let currentData = getCurrentData();
		restData = currentData;
		uploadHistory(restData);
		return restData;
	}
	console.log("----------缓存",historyIndex)
	restData = history[historyIndex];
	console.log(restData);
	util.dateSimpleOperation(restData.currentDate,-1);
	return restData;
}


/* 得到当天数据 */
function getCurrentData()
{
	const currentDate = new Date();
	
	let currentData = {
		"logo":getCurrentlogo(undefined,"-"),
		"currentDate":currentDate,
		"mid":[],
		"lat":[],
		"week":currentDate.getDay(),
		"chineseTime":util.dateToCastChineseTime(currentDate),
	};
	let history = getHistory();
	let footlist = showFoot(currentData);
	
	if(history.length != 0)
	{
		let filterHistory = history.filter(e=> e.logo == getCurrentlogo(util.dateSimpleOperation(currentData.currentDate,-1),"-") || e.logo == getCurrentlogo(util.dateSimpleOperation(currentData.currentDate,-2),"-"));
		console.log("-------------------------去重");
		console.log(history);
		console.log(footlist);
		for(let [k,v] of filterHistory.entries())
		{
		 
			for( let [k1,v1] of v.mid.entries())
			{
				console.log(v1);
				let delIndex =  v1 ? footlist.findIndex(e=> e.name == v1.name) : -1;
				if(delIndex !=-1)
				{
					footlist.splice(delIndex,1);
				}
				
			}
			for( let [k1,v1] of v.lat.entries())
			{
				console.log(v1);
				let delIndex = v1 ? footlist.findIndex(e=> e.name == v1.name) : -1;
				if(delIndex !=-1)
				{
					footlist.splice(delIndex,1);
				}
			}
		}
		console.log(footlist);
	}
	let meats = footlist.filter(e=> e.nutritionType == "meat");
	let plains = footlist.filter(e=> e.nutritionType =="plain");
	
	
	/* Array.prototype.shuffle = function() {
	    var array = this;
	    var m = array.length,
	        t, i;
	    while (m) {
	        i = Math.floor(Math.random() * m--);
	        t = array[m];
	        array[m] = array[i];
	        array[i] = t;
	    }
	    return array;
	} */
	shuffle(meats); shuffle(plains);
	let midmeats = meats.filter(e=> e.type=="mid");
	let latmeats = meats.filter(e=> e.type=="lat");
	let midplains = plains.filter(e=> e.type=="mid");
	let latplains = plains.filter(e=> e.type=="lat");
	currentData.mid.push(midmeats[0]);currentData.mid.push(midmeats[1]);currentData.mid.push(midplains[0]);
	currentData.lat.push(latplains[0]);currentData.lat.push(latmeats[0]);currentData.lat.push(latplains[1]);
	return currentData;
}

/* 导入foot */
function import_foot_data(foot)
{
	let all_foot_data = exports_foot_data();
	
	console.log(all_foot_data);
	
	let list = all_foot_data.get(foot.type);
	
	list.push(foot);
	
	all_foot_data.set(foot.type,list);
	
	operation_all_foot_data(all_foot_data);
	
}

/* 更新foot */
function remove_foot_data(foot){
	
	let all_foot_data = exports_foot_data();
	
	console.log("删除前的all_foot_data",all_foot_data);
	
	console.log("要删除的foot :",foot);
	
	let list = all_foot_data.get(foot.type);
	list.splice(list.findIndex(e=> e.name ===foot.name));
	
	all_foot_data.set(foot.type,list);
	operation_all_foot_data(all_foot_data);

	
}

/* 导出foot */
function exports_foot_data()
{
	let all_foot_data = new Map();
	
	uni.getStorage({
	    key: 'all_foot_data',
	    success: function (res) {
	        console.log(res.data);
			all_foot_data = new Map(res.data);
	    },
		fail(e) {
			console.log(e,"本地数据获取失败,数据可能不存在或被清空.");
			all_foot_data.set("top",[]);
			all_foot_data.set("mid",[]);
			all_foot_data.set("lat",[]);
		}
	});
	
	return all_foot_data;
	
}


/* 计算foot */
function calculate_week_foot_data()
{
	const aDay = 1000*60*60*24;
	
	let currentDate = new Date();
	let all_foot_with_data = operation_diy_data("all_foot_with_data");
	
	let currentData = null;
	
	currentData = all_foot_with_data.get(getCurrentlogo());
	
	if(currentData!=undefined || currentData!=null)
	{
		//直接返回当前foot
		return currentData;
	}
	
	
	let beforeDate1 = new Date(currentDate.getTime()-aDay);
	let beforeDate2 = new Date(beforeDate1.getTime()-aDay);
	
	
	
	let b1 = all_foot_with_data.get(getDiyLogo(beforeDate1));
	
	let b2 = all_foot_with_data.get(getDiyLogo(beforeDate2));
	
	let all_foot_data = exports_foot_data();
	
	
	
	let tops = all_foot_data.get("top");
	let mids = all_foot_data.get("mid");
	let lats = all_foot_data.get("lat");
	
	tops.splice(tops.findIndex(e=> e.name === b1.top || e.name === b2.top));
	mids.splice(mids.findIndex(e=> e.name === b1.mid || e.name === b2.mid));
	lats.splice(lats.findIndex(e=> e.name === b1.lat || e.name === b2.lat));
	
}

function shuffle(arr) {
	let i = arr.length;
	while (i) {
	    let j = Math.floor(Math.random() * i--);
	    [arr[j], arr[i]] = [arr[i], arr[j]];
	}
}



module.exports = {
	uploadFoot: uploadFoot,
	showFoot: showFoot,
	removeFoot: removeFoot,
	currentRecord: currentRecord,
	showHistory: showHistory,
}